#include "pasting_color.h"

color_tab *
add_color (color_tab * tete, char *k, char *c)
{
  color_tab *niou = (color_tab *) malloc (sizeof (color_tab));
  niou->next = tete;
  niou->key_word = (char *) malloc (strlen (k) + 1);
  strcpy (niou->key_word, k);
  niou->key_color = (char *) malloc (strlen (c) + 1);
  strcpy (niou->key_color, c);
  return niou;
}

void
free_color_tab (color_tab * tete)
{
  color_tab *run, *tmp;
  for (run = tete; run; run = tmp)
    {
      tmp = run->next;
      free (run->key_color);
      free (run->key_word);
      free (run);
    }
}

color_tab *
load_color_tab (char *filename)
{
  color_tab *tete = NULL;
  char bread[151], *pread;
  FILE *fa;
  fa = fopen (filename, "r");
  if (!fa)
    return tete;

  while (1)
    {
      fgets (bread, 150, fa);
      if (feof (fa))
	break;
      for (pread = bread; *pread != '=' && *pread != 0; pread++);
      if (*pread != '=')
	continue;
      *pread = 0;
      pread++;
      clean_txt (pread);
      tete = add_color (tete, bread, pread);
    }
  fclose (fa);
  return tete;
}


char *
keyword_ou_pas (char *mot)
{
  color_tab *run;
  for (run = color; run; run = run->next)
    if (!strcmp (run->key_word, mot))
      return run->key_color;
  return NULL;
}
